# stages.py

# Possibly interesting values:
#     CONTENT_LENGTH:
#     CONTENT_TYPE: application/x-www-form-urlencoded
#     PATH_INFO: /hello/mom/and/dad.html
#     QUERY_STRING: this=value&that=too
#     REMOTE_ADDR: 127.0.0.1
#     REQUEST_METHOD: GET
#     SCRIPT_NAME:
#     HTTP_ACCEPT_LANGUAGE: en-us,en;q=0.5
#     wsgi.errors: <file>
#     wsgi.file_wrapper: <file>
#     wsgi.input: <file to read request body>
#     wsgi.multiprocess: False
#     wsgi.multithread: True
#     wsgi.run_once: False

import os
import webob
import operator

# Naimath engine
from naimath.engine import engine, question

Debug = False

def init(path, supers):
    global Rootdir, Localedir, Filesdir
    path = os.path.abspath(path)
    if not os.path.isdir(path): path = os.path.dirname(path)
    for i in range(supers): path = os.path.dirname(path)
    Rootdir = path
    Localedir = os.path.join(path, 'translation', '0.1')
    Filesdir = os.path.join(path, 'files')
    if Debug: print "Rootdir", Rootdir
    if Debug: print "Localedir", Localedir
    if Debug: print "Filesdir", Filesdir

# Set default Localedir:
init(__file__, 2)

def load_translations(req, *domains):
    languages = req.accept_language.best_matches()
    if Debug: print "languages", languages
    question.load_translations(Localedir, languages, *domains)

def reader(file):
    for line in file:
        if '#' in line: line = line[:line.index('#')]
        line = line.strip()
        if line: yield line

def index(req):
    with open(os.path.join(Filesdir, 'static', 'index.html'), 'rt') as f:
        data = f.read()
    return webob.Response(request=req, status="200 OK",
                          content_type="text/html", body=data)

def canned_questions(req):
    if Debug: print "PATH_INFO", req.path_info
    if Debug: print "SCRIPT_NAME", req.script_name

    load_translations(req, 'question')

    from naimath.web.templates import canned
    t = canned.canned()
    t.title = 'Canned Questions'
    t.operator = operator
    t.filesdir = Filesdir

    with open(os.path.join(Filesdir, 'canned_questions',
                           req.path_info.lstrip('/')),
              'rt') \
      as f:
        t.questions = [question.lookup(q) for q in reader(f)]

    return webob.Response(request=req, status="200 OK",
                          content_type="text/html", body=str(t))

def symptoms(req):
    load_translations(req, 'diagnosis', 'question')
    from naimath.web.templates import symptoms
    t = symptoms.symptoms()
    t.title = 'Symptoms'
    t.phase = 'symptoms'
    populate(t, req)
    return webob.Response(request=req, status="200 OK",
                          content_type="text/html", body=str(t))

def get_answers(req):
    ans = req.params
    if Debug: print "answers", ans
    return ans

def populate(template, req):
    template.operator = operator
    template.filesdir = Filesdir
    answers = get_answers(req)
    if Debug: print "answers", answers
    if Debug: print "dict(answers)", dict(answers)
    if Debug: print "answers.items()", answers.items()
    template.answers = answers.items()
    template.diagnosis_list = \
      sorted(((question.Map['diagnosis'](d), cf,
               sorted(((question.lookup(q), w) for q, w in q_list),
                      key = lambda t: t[1],   # weight
                      reverse = True))
              for d, cf, q_list
               in engine.consult('pulmonary', question=answers)),
             key = lambda x: x[1],
             reverse = True)

